var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            ID: 0,
            content: '',
            Type: 1,//1-暂停 2-流转 3 到期提醒时间调整
            action:'customerpausestartstatus',
            IsStopSend:false,
            StopSendTime:'',
        },
        placeholder:'请填写工单暂停原因(选填信息)',
        DelayID: 0,
        DelayTime: '',
        DelayTimeDiff: 1,
        timeModel: {ID: 1, time: 1 },
        timelist: [{ ID: 1, time: 1 }, { ID: 2, time: 2 },],
        StopSendTimes:"",
    },
    created() {
        this.form.StopSendTime = moment().format('YYYY-MM-DD HH:mm');
        this.onChooseDelayTime(this.timelist[0]);
    },
    methods: {
        onSelectStop(){
            this.form.IsStopSend=! this.form.IsStopSend;
            if(this.form.IsStopSend){
                setTimeout(function(){
                    app.initPicker();
                },100);
            }
        },
        onChooseDelayTime(item) {
            var that = this;
            that.timeModel = item;
            that.DelayTimeDiff = item.time;
            if (that.form.StopSendTime == "") {
                that.form.StopSendTime = moment(new Date()).format('YYYY-MM-DD HH:mm');
            }
            that.DelayTime = moment(new Date(that.form.StopSendTime)).add(item.time, 'd').format('YYYY-MM-DD HH:mm');
            that.form.StopSendTime = that.DelayTime;
        },
        onSave: function() {
            var that = this;
            if(that.form.Type==1&&that.form.IsStopSend){
                const diffStartTime = moment(that.form.StopSendTime).diff(moment(new Date()), 'seconds');
                if (diffStartTime <= 0) {
                   api.toast({
                       msg: '选择的到期时间不能小于当前系统时间',
                       duration: 2000,
                       location: 'bottom'
                   });
                   return;
               }
            }
            ns.post(that.form, function(succeed, data, err) {
                if (succeed) {
                    if(data.status){
                        ns.toast('操作成功');
                        api.sendEvent({
                            name: 'customerservicelist_frm',
                        });
                        api.sendEvent({
                            name: 'reloadservicelist',
                        });
                        api.sendEvent({
                            name: 'updata_task_con',
                            extra: {
                                key1: 'value1'
                            }
                        });
                        setTimeout(function() {
                            api.closeWin();
                        }, 1000);
                        return;
                    }
                    else if (data.error) {
                        api.toast({
                            msg: data.error,
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        onSaveTime: function() {
            var that=this;
            if(that.form.StopSendTime==""){
                api.toast({
                    msg: '请选择到期提醒时间',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            const diffStartTime = moment(that.form.StopSendTime).diff(moment(new Date()), 'seconds');
            if (diffStartTime <= 0) {
               api.toast({
                   msg: '选择的到期时间不能小于当前系统时间',
                   duration: 2000,
                   location: 'bottom'
               });
               return;
           }
            var options = {
                action: 'customerserviceeditstoptime',
                ID: that.form.ID,
                StopSendTime: moment(that.form.StopSendTime).format('YYYY-MM-DD HH:mm'),
            };
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    if(data.status){
                        ns.toast('操作成功');
                        api.sendEvent({
                            name: 'customerservicelist_frm',
                        });
                        api.sendEvent({
                            name: 'reloadservicelist',
                        });
                        api.sendEvent({
                            name: 'updata_task_con',
                            extra: {
                                key1: 'value1'
                            }
                        });
                        setTimeout(function() {
                            api.closeWin();
                        }, 1000);
                        return;
                    }
                    else if (data.error) {
                        api.toast({
                            msg: data.error,
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_save: function() {
            var that = this;
            if (that.form.Type == 3) {
                that.onSaveTime();
                return;
            }
            that.onSave();
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    var btn = document.querySelector('#choosedatetime');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.form.StopSendTime){
                                _self.picker.setSelectedValue(_this.form.StopSendTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.form.StopSendTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.form.StopSendTime){
                                _self.picker.setSelectedValue(_this.form.StopSendTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.StopSendTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);

                });
            })(mui, document);
        },
    }
});

apiready = function() {
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.form.ID = api.pageParam.id || 0;
    app.form.Type=api.pageParam.type; 
    app.StopSendTimes=api.pageParam.StopSendTime||"";
    if(app.form.Type==2){
        app.placeholder='请填写工单流转原因(选填信息)';
    }
    //到期提醒调整
    if (app.form.Type == 3) {
        app.form.IsStopSend = true;
        app.form.StopSendTime = "";
        app.timeModel = {};
        if (app.StopSendTimes!="") {
            app.form.StopSendTime = app.StopSendTimes;
        }
        setTimeout(function(){
            app.initPicker();
        },100);
    }
}
